import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/views/index.vue'
import About from '@/views/about.vue'
import Wiki from '@/views/wiki/index.vue'

import { config } from '@/config/config'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      component: About
    },
    {
      path: '/wiki',
      name: 'wiki',
      component: Wiki
    },
    // router/index.ts
    {
      path: '/wiki/:wiki',
      component: () => import('@/views/wiki/layout/index.vue'),
      children: [
        {
          path: ':articleId',
          name: 'wikiDetail',
          component: () => import('@/views//wiki/layout/WikiDetail.vue')
        }
      ]
    },
    // {
    //   path: '/wiki/:wiki/:articleId',
    //   name: 'wikiDetail',
    //   component: () => import('@/views/wiki/WikiDetail.vue'),
    //   meta: {
    //     title: '详情'
    //   }
    // },
    {
      path: '/article/:id',
      name: 'ArticleDetail',
      component: () => import('@/views/ArticleDetail.vue'),
      meta: {
        title: '文章详情'
      }
    }
  ]
})
router.beforeEach((to, from, next) => {
  const routeTitle = to.meta.title as string

  if (routeTitle) {
    document.title = `${config.baseTitle} | ${routeTitle}`
  } else {
    document.title = config.baseTitle
  }

  next()
})

export default router
